<?php
namespace SimplePhp\Database;
use SimplePhp\Database\IModel;
use SimplePhp\System\TInject;

/**
 *
 */
class Model implements IModel {
	use TInject;

	const fetch_arr_number = \PDO::FETCH_NUM;
	const fetch_arr_key = \PDO::FETCH_ASSOC;
	const fetch_arr_numberAkey = \PDO::FETCH_BOTH;
	const fetch_obj = \PDO::FETCH_CLASS;

	private $mode = self::fetch_obj, $classname = "SimplePhp\Database\DbUnit";
	/**
	 * @Inject[di=DbHelp]
	 * @var [type]
	 */
	protected $help;

	/**
	 * @Inject[di=IConstraint]
	 * @var [type]
	 */
	protected $IConstraintBuild;

	public function insert($Entiy) {
		$this->IConstraintBuild = new MysqlConstraintBuild();
		$sql = $this->IConstraintBuild->buildInsertSql($Entiy);
		var_dump($sql);
		$return = $this->help->exec($sql, $this->IConstraintBuild->getBindData());
		return $return;
	}
	/**
	 * 聚合语句count sun avg等
	 * group by
	 * order by
	 * 函数使用
	 * having
	 * 子查询
	 * 表链接
	 * @param  [type] $entiy [description]
	 * @return [type]        [description]
	 */
	public function select($Entiy) {
		$this->IConstraintBuild = new MysqlConstraintBuild();
		$sql = $this->IConstraintBuild->buildSelectSql($Entiy);
		var_dump($sql);
		// $sql="select  cat.id as cat_id,cat.name as cat_name from video_cat as cat;"
		$std = $this->help->setFetchMode($this->mode, $this->classname)->query($sql, $this->IConstraintBuild->getBindData());
		$data = [];
		while ($result = $std->fetch($this->mode)) {
			$data[] = $result;
		}
		return $data;
	}
	public function update($Entiy) {
		$this->IConstraintBuild = new MysqlConstraintBuild();
		$sql = $this->IConstraintBuild->buildUpdataSql($Entiy);
		var_dump($sql);
		$return = $this->help->exec($sql, $this->IConstraintBuild->getBindData());
		return $return;
	}
	public function delete($Entiy) {}
	public function find() {}
	public function qurey() {}
	public function exec() {}
	public function lazySelect() {}

	/**
	 * 遍历数据的模式
	 * @param [type] $mode      [description]
	 * @param [type] $classname [description]
	 */
	public function setFetchMode($mode, $classname = null) {
		$this->mode = $mode;
		$this->classname = empty($classname) ? $this->classname : $classname;
		return $this;
	}
}